<?php

namespace app\index\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

class Index extends Common
{
    /**
     * 首页
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 获取session中的数据
        $id = Session::get("id") ? Session::get("id") : 0;
        $username = Session::get("username");
        // 新品上架
        $news = Db::table("shop_goods")->where("is_new",1)->field(["id","title","image","intro","price"])->limit(5)->select();
        // 热卖商品
        $hots = Db::table("shop_goods")->where("is_hot",1)->field(["id","title","image","intro","price"])->limit(5)->select();
        // 精品
        $sales = Db::table("shop_goods")->where("sales",">",100)->field(["id","title","image","intro","price"])->limit(5)->select();
        // 推荐
        $goods = Db::table("shop_goods")->order('id', 'desc')->field(["id","title","image","intro","price"])->limit(5)->select();
        // 分类
        $cats = Db::table("shop_cat")->where("pid","0")->where("status",1)->field(["id","name"])->limit(12)->select();

        // 多級分類
        $list = Db::table("shop_cat")->select();
        $tree = $this->getNavData($list);
//        foreach ($tree as $v){
//            var_dump($v['child']);
//        }
//        var_dump($tree);exit;

        MyViewAssign([
            "login" => $username,
            "id" => $id,
            "news" => $news,
            "hots" => $hots,
            "sales" => $sales,
            "goods" => $goods,
            "cats" => $cats,
            "tree" => $tree,
        ]);
        return MyView();
    }

    /**
     * 获取所有商品
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsList()
    {
        $goods = Db::table("shop_goods")->field(["id","title","image"])->select()->toArray();
        echo json_encode(['code'=>0,'msg'=>'查询成功','goods'=>$goods]);exit;
    }

    /**
     * 获取商品信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        if (Request::method() == "POST"){
            $all = Request::param();
            $good = Db::table("shop_goods")->where("id",$all['id'])->find();
            echo json_encode(['code'=>0,'msg'=>'查询成功','good'=>$good]);exit;
        }
    }

    /**
     * 商品列表页
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list()
    {
        // 新品推荐
        $goods = Db::table("shop_goods")->order('id', 'desc')->field(["id","title","image","intro","price"])->limit(3)->select();
        // 热销商品
        $hots = Db::table("shop_goods")->where("is_hot",1)->field(["id","title","image","intro","price","discount"])->limit(3)->select();
        // 分类
        $cats = Db::table("shop_cat")->where("pid","0")->where("status",1)->field(["id","name"])->limit(12)->select();
        MyViewAssign([
            "goods" => $goods,
            "hots" => $hots,
            "cats" => $cats,
        ]);
        return MyView();
    }

    /**
     * 商品详情页
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $uid = Session::get("id");
        $id = Request::param("id");
        $good = Db::table("shop_goods")
                ->alias("g")
                ->where("g.id",$id)
                ->join(["shop_cat" => "c"],"g.cat = c.id")
                ->field(["g.*","c.name"])
                ->find();
        $good["add_time"] = date("Y-m-d H:i:s",$good["add_time"]);
        MyViewAssign([
            "uid" => $uid,
            "good" => $good,
        ]);
        return MyView();
    }
}